package com.milhome;

import java.util.ArrayList;

import org.apache.http.NameValuePair;
import org.apache.http.message.BasicNameValuePair;

import com.milhome.conexao.ConexaoHttpClient;

import android.app.Activity;
import android.app.AlertDialog;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;


public class ServerJSPActivity extends Activity {

	EditText user, senha;
	Button login;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);

		user = (EditText) findViewById(R.edit.usuario);
		senha = (EditText) findViewById(R.edit.senha);
		login = (Button) findViewById(R.botao.logar);

		login.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View v) {
				Log.i("LOGAR", "BUTAO PRESSIONADO");
				String urlPost = "http://192.168.0.123:8080/AndroidWeb/logar.jsp";
				String urlGet = "http://192.168.0.123:8080/AndroidWeb/logar.jsp?nomeP="+ user.getText().toString() + "&senhaP="+ senha.getText().toString();
				ArrayList<NameValuePair> parametrosPost = new ArrayList<NameValuePair>();
				parametrosPost.add(new BasicNameValuePair("nomeP", user.getText().toString()));
				parametrosPost.add(new BasicNameValuePair("senhaP", senha.getText().toString()));
				String resposta = null;
				Log.i("LOGAR", "VAI ENTRAR NO TRY");
				try {
					Log.i("LOGAR", "ENTROU NO TRY...");
					resposta = ConexaoHttpClient.exeHttpPost(urlPost, parametrosPost);
//					resposta = ConexaoHttpClient.exeHttpGet(urlPost);
					Log.i("LOGAR", "RESULT......................................................." );
					String result = resposta.toString();
					Log.i("LOGAR", "RESULT..." + result.toString());
					result = result.replaceAll("\\s+", "");
					if(result.equals("Logado com Sucesso."))
						mensagem("login", "Logado");
					else
						mensagem("login", "User invalido");

				} catch (Exception e) {
					Toast.makeText(ServerJSPActivity.this, "Erro: " + e,
							Toast.LENGTH_LONG);
				}

			}
		});

	}
	
	public void mensagem (String titulo, String texto){
		AlertDialog.Builder men = new AlertDialog.Builder(ServerJSPActivity.this);
		men.setTitle(titulo);
		men.setMessage(texto);
		men.setNeutralButton("ok", null);
		men.show();
	}
}